package com.baseandroid.dagger.module;

import com.androidbase.dagger.help.RetrofitHelper;
import com.baseandroid.config.Api;
import com.baseandroid.config.BossApi;
import com.baseandroid.config.ContractApi;
import com.baseandroid.config.MyApi;
import com.baseandroid.config.UploadApi;
import com.baseandroid.dagger.qualifier.BossUrl;
import com.baseandroid.dagger.qualifier.ContractUrl;
import com.baseandroid.dagger.qualifier.MyUrl;
import com.baseandroid.dagger.qualifier.UploadUrl;
import com.baseandroid.dagger.scope.MyAppScope;
import com.baseandroid.retrofit.MyOkhttpHelper;

import java.util.concurrent.TimeUnit;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

@Module
public class MyAppModule {

    @MyAppScope
    @Provides
    @MyUrl
    Retrofit provideMyUrlRetrofit(MyOkhttpHelper okhttpHelper, RetrofitHelper retrofitHelper) {
        return createMyApiRetrofit(okhttpHelper, retrofitHelper, Api.sServerApiUrl);
    }

    @MyAppScope
    @Provides
    @BossUrl
    Retrofit provideBossUrlRetrofit(MyOkhttpHelper okhttpHelper, RetrofitHelper retrofitHelper) {
        return createBossApiRetrofit(okhttpHelper, retrofitHelper, Api.sBalanceApiUrl);
    }

    @MyAppScope
    @Provides
    @ContractUrl
    Retrofit provideContractUrlRetrofit(MyOkhttpHelper okhttpHelper, RetrofitHelper retrofitHelper) {
        return createContractApiRetrofit(okhttpHelper, retrofitHelper, Api.sContractApiUri);
    }

    @MyAppScope
    @Provides
    @UploadUrl
    Retrofit provideUploadUrlRetrofit(MyOkhttpHelper okhttpHelper, RetrofitHelper retrofitHelper) {
        return createUploadApiRetrofit(okhttpHelper, retrofitHelper, Api.sUpLoadingUrl);
    }

    private Retrofit createMyApiRetrofit(MyOkhttpHelper okhttpHelper, RetrofitHelper retrofitHelper, String url) {
        Retrofit.Builder builder = retrofitHelper.getRetrofit().newBuilder();
        OkHttpClient.Builder client = okhttpHelper.getHttpClient().newBuilder();
        return builder.baseUrl(url).client(client.build()).build();
    }

    private Retrofit createBossApiRetrofit(MyOkhttpHelper okhttpHelper, RetrofitHelper retrofitHelper, String url) {
        Retrofit.Builder builder = retrofitHelper.getRetrofit().newBuilder();
        OkHttpClient.Builder client = okhttpHelper.getHttpClient().newBuilder();
        return builder.baseUrl(url).client(client.build()).build();
    }

    private Retrofit createContractApiRetrofit(MyOkhttpHelper okhttpHelper, RetrofitHelper retrofitHelper, String url) {
        Retrofit.Builder builder = retrofitHelper.getRetrofit().newBuilder();
        OkHttpClient.Builder client = okhttpHelper.getHttpClient().newBuilder();
        client.connectTimeout(30, TimeUnit.SECONDS);
        client.readTimeout(30, TimeUnit.SECONDS);
        client.writeTimeout(30, TimeUnit.SECONDS);
        return builder.baseUrl(url).client(client.build()).build();
    }

    private Retrofit createUploadApiRetrofit(MyOkhttpHelper okhttpHelper, RetrofitHelper retrofitHelper, String upLoadingUrl) {
        Retrofit.Builder builder = retrofitHelper.getRetrofit().newBuilder();
        OkHttpClient.Builder client = okhttpHelper.getHttpClient().newBuilder();
        return builder.baseUrl(upLoadingUrl).client(client.build()).build();
    }

    @MyAppScope
    @Provides
    MyApi provideMyApi(@MyUrl Retrofit retrofit) {
        return retrofit.create(MyApi.class);
    }

    @MyAppScope
    @Provides
    BossApi providBossApi(@BossUrl Retrofit retrofit) {
        return retrofit.create(BossApi.class);
    }

    @MyAppScope
    @Provides
    ContractApi provideContractApi(@ContractUrl Retrofit retrofit) {
        return retrofit.create(ContractApi.class);
    }

    @MyAppScope
    @Provides
    UploadApi provideUploadApi(@UploadUrl Retrofit retrofit) {
        return retrofit.create(UploadApi.class);
    }
}
